/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  FangWeiBoUtil.java   
 * @Package com.swift.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-27 下午5:40:56   
 * @version V1.0     
 */
package com.swift.maintenance.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.jiyi.bean.zone.Topic;
import com.swift.widget.MyClickableSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: FangWeiBoUtil
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-27 下午5:40:56
 * 
 */
public class FangWeiBoUtil {

	// 定义正则表达式
	private static final String AT = "@[\u4e00-\u9fa5\\w]+";// @人
	private static final String TOPIC = "#[\u4e00-\u9fa5\\w]+#";// ##话题
	private static final String EMOJI = "\\[[\u4e00-\u9fa5\\w]+\\]";// 表情
	private static final String URL = "http://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";// url

	public static SpannableString getWeiBoContent(final Context context,
			final Topic bean, final TextView textView) {
		SpannableString spannableString = new SpannableString(bean.getContent());
		// 设置正则
		Pattern pattern = Pattern.compile(TOPIC);
		Matcher matcher = pattern.matcher(spannableString);

		if (matcher.find()) {
			// 要实现文字的点击效果，这里需要做特殊处理
			textView.setMovementMethod(LinkMovementMethod.getInstance());
			// 重置正则位置
			matcher.reset();
		}

		while (matcher.find()) {
			final String topic = matcher.group();
			// 处理话题##符号
			if (topic != null) {
				int start = matcher.start();
				int end = start + topic.length();

				// 设置变色
				spannableString.setSpan(new ForegroundColorSpan(Color.BLUE),
						start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

				// 设置可点击
				MyClickableSpan clickableSpan = new MyClickableSpan() {

					@Override
					public void onClick(View widget) {
//						Toast.makeText(context, "点击了话题：" + topic,
//								Toast.LENGTH_LONG).show();
//						Intent intent = new Intent(textView.getContext(),MainActivity.class);
//						textView.getContext().startActivity(intent);
					}
				};
				spannableString.setSpan(clickableSpan, start, end,
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

		}
		return spannableString;
	}

}
